﻿using AutoMapper;
using BW.ME.Exam.Domain.Entities;
using BW.ME.Exam.ErrorCode;
using BW.ME.Exam.Infrastructure.Interfaces;
using BW.ME.Exam.Write.Api.Applications.Commands.ClassCreateCommand;
using MediatR;
using System.Transactions;
using Yitter.IdGenerator;

namespace BW.ME.Exam.Write.Api.Applications.CommandHandlers.GClassCommands
{
    /// <summary>
    /// 表单创建命令处理
    /// </summary>
    public class ClassCreateCommandHandler : IRequestHandler<ClassCreateCommand, ApiResult<object>>
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="GClassRepository"></param>
        /// <param name="InstanceRepository"></param>
        /// <param name="RecordRepository"></param>
        public ClassCreateCommandHandler(IBaseRepository<GClass> GClassRepository,IBaseRepository<WF_Instance> InstanceRepository,IBaseRepository<WF_Record> RecordRepository,IMapper mapper,IBaseRepository<WF_Setting> SettingRepository)
        {
            this.GClassRepository = GClassRepository;
            this.InstanceRepository = InstanceRepository;
            this.RecordRepository = RecordRepository;
              Mapper = mapper;
            this.SettingRepository = SettingRepository;
        }
        /// <summary>
        /// 班级仓储
        /// </summary>
        public IBaseRepository<GClass> GClassRepository { get; }
        /// <summary>
        /// 实例仓储
        /// </summary>
        public IBaseRepository<WF_Instance> InstanceRepository { get; }
        /// <summary>
        /// 流转记录仓储
        /// </summary>
        public IBaseRepository<WF_Record> RecordRepository { get; }
        public IMapper Mapper { get; }
        public IBaseRepository<WF_Setting> SettingRepository { get; }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<ApiResult<object>> Handle(ClassCreateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();
            result.Code = ResultCode.添加成功;
            result.Message=ResultCode.添加成功.ToString();

            using (TransactionScope scope=new TransactionScope())
            {
                //异常处理
                try
                {
                    //查询流转设置
                    var settings = SettingRepository.GetAll().ToList();
                    //表单提交
                    GClass gClass = Mapper.Map<GClass>(request);
                    GClassRepository.Add(gClass);
                 
                    DateTime now= DateTime.Now;
                    string InstanceId=YitIdHelper.NextId().ToString();
                   //实例
                    WF_Instance instance = new WF_Instance();
                    instance.InstanceId= InstanceId;
                    instance.AppId = gClass.ClassId;
                    instance.FlowStatus=(int)FlowStatus.申请;
                    instance.FlowStatusName=FlowStatus.申请.ToString();
                    instance.StaterId = request.CurrentUserId;
                    instance.StarterName = request.CurrentUserNickname;
                    instance.CurrentNodeNo = settings[0].NodeNo;
                    instance.CurrentNodeName = settings[0].NodeName;
                    instance.CurrentOperatorId = request.CurrentUserId;
                    instance.CurrentOperatorName = request.CurrentUserNickname;
                    instance.NextNodeNo = settings[1].NodeNo;
                    instance.NextNodeName = settings[1].NodeName;
                    instance.NextOperatorId = settings[1].UserId;
                    instance.NextOperatorName = settings[1].UserNickname;
                    instance.CreateTime = now;
                    InstanceRepository.Add(instance);


                   

                    #region 流转记录
                    WF_Record record = new WF_Record();
                    record.InstanceId = InstanceId;
                    record.NodeNo = settings[0].NodeNo;
                    record.NodeName = settings[0].NodeName;
                    record.OperatorId = request.CurrentUserId;
                    record.OperatorName = request.CurrentUserNickname;
                    record.OperatorOpion = "";
                    record.ApproveStatus = (int)SPStatus.已提交;
                    record.ApproveStatusName=SPStatus.已提交.ToString();
                    record.CreateTime= now;
                    RecordRepository.Add(record);
                    #endregion

                    //提交
                    scope.Complete();
                }
                catch (Exception exp)
                {
                    result.Code = ResultCode.添加失败;
                    result.Message = ResultCode.添加失败.ToString();
                }
            }

                return Task.FromResult(result);
        }
    }
}
